//
//  MHBaseViewController.m
//  ManHua
//
//  Created by 赵永泉 on 15/10/24.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHBaseViewController.h"
#import "UIImage+Addiction.h"
#import "MHUserInfo.h"
#import "MHUserInfoSington.h"
#import <RESideMenu.h>
#import <SDWebImageManager.h>

@interface MHBaseViewController ()

@end

@implementation MHBaseViewController

- (void)dealloc
{
    NSLog(@"%@+++++++++++++dealloc", NSStringFromClass(self.class));
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:[self createLeftBarButton]];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(isLogin:) name:@"login" object:nil];
}

- (void)isLogin:(NSNotification *)notification
{
    if ([self isEqual:self.navigationController.viewControllers[0]])
    {
        UIButton *button = self.navigationItem.leftBarButtonItem.customView;
        if ([MHUserInfoSington sharedInstance].userInfo != nil && [MHUserInfoSington sharedInstance].userInfo.icon != nil) {
            [button setImage:[UIImage circleImageWithImage:[MHUserInfoSington sharedInstance].userInfo.icon borderWidth:0 borderColor:[UIColor clearColor]] forState:UIControlStateNormal];
        }
        else
        {
            [button setImage:[UIImage circleImageWithImage:[UIImage imageNamed:@"akalin.jpg"] borderWidth:0 borderColor:nil] forState:UIControlStateNormal];
        }
    }

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([self isEqual:self.navigationController.viewControllers[0]])
    {
        self.navigationController.tabBarController.navigationController.sideMenuViewController.panGestureEnabled = YES;
        UIButton *button = self.navigationItem.leftBarButtonItem.customView;
        if ([MHUserInfoSington sharedInstance].userInfo != nil && [MHUserInfoSington sharedInstance].userInfo.icon != nil) {
            [button setImage:[UIImage circleImageWithImage:[MHUserInfoSington sharedInstance].userInfo.icon borderWidth:0 borderColor:[UIColor clearColor]] forState:UIControlStateNormal];
        }
        else
        {
            [button setImage:[UIImage circleImageWithImage:[UIImage imageNamed:@"akalin.jpg"] borderWidth:0 borderColor:nil] forState:UIControlStateNormal];
        }
    }
    else
    {
        self.navigationController.tabBarController.navigationController.sideMenuViewController.panGestureEnabled = NO;
    }
    if ([self.navigationController isEqual:self.sideMenuViewController.contentViewController]) {
        self.navigationController.navigationBarHidden = NO;
    }
    else
    {
        ((UINavigationController *)self.sideMenuViewController.contentViewController).navigationBarHidden = YES;
    }
}

- (UIView *)createLeftBarButton
{
    CGFloat buttonW = 35;
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, buttonW, buttonW)];
    if ([self isEqual:self.navigationController.viewControllers[0]]) {
        [button setImage:[UIImage circleImageWithImage:[UIImage imageNamed:@"akalin.jpg"] borderWidth:0 borderColor:nil] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(changeFrame) forControlEvents:UIControlEventTouchUpInside];
    }
    else
    {
        button.frame = CGRectMake(0, 0, 15, 15);
        [button setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(backPreviousVC) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
    return button;
}

- (void)changeFrame
{
    [self.sideMenuViewController presentLeftMenuViewController];
}

- (void)backPreviousVC
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
